﻿Shader "Dan/UI/UI_NetGrid"
{
    Properties
    {
       _Color ("Color", Color) = (0.07843138,0.3921569,0.7843137,1)
        _Emission ("Emission", Float ) = 1
        _U ("U", Float ) = 4
        _V ("V", Float ) = 4
        _Grid_Width ("Grid_Width", Range(0, 1)) = 0.04273506
        _EdgeMask ("Edge Mask", Range(0, 1)) = 0.2
        _Grid_Opacity ("Grid_Opacity", Float ) = 1
        _BG_Opacity ("BG_Opacity", Range(0, 1)) = 0.1
        _RandomSpeed ("RandomSpeed", Float ) = 1
        _ShinnyAmplitude ("ShinnyAmplitude", Range(0, 1)) = 0.5
    }
    SubShader
    {
        Tags { "IgnoreProjector"="True"
            "Queue"="Transparent"
            "RenderType"="Transparent" }
        LOD 100

        Pass
        {
            
            Blend SrcAlpha OneMinusSrcAlpha
            ZWrite Off
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
               
                float4 vertex : SV_POSITION;
            };

             float4 _Color;
             float _U;
             float _V;
             float _Grid_Width;
             float _Grid_Opacity;
             float _EdgeMask;
             float _Emission;
             float _BG_Opacity;
             float _RandomSpeed;
             float _ShinnyAmplitude;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                
                return o;
            }

            float  DrawCurvedRect (float2 uv , float size , float blur){
                uv =(1.0 - uv)*uv*size;
                float shape = smoothstep( 0, blur, saturate(uv.x*uv.y)); 
                return shape;
            }
            
            float Flicker01 (float gradient , float speed, float amplitude){
                float2 rd = float2(gradient,gradient)+_Time.y*0.001*speed;
                float2 sk = rd + 0.2127+rd.x*0.3713*rd.y;
                float2 randomVal = 4.789*sin(489.123*(sk));
                float rdSpeed = frac(randomVal.x*randomVal.y*(1+sk.x));
                float flick = saturate( ( rdSpeed  * 0.5 ) / amplitude +0.5);
                return flick;
            }

            float DrawNet (float2 uv , float2 netUV , float width){
                netUV = frac(netUV*uv)*2-1;
                float net = saturate(max(abs(netUV.x),abs(netUV.y)));
                return step(1.0 - width,net);
            }


            fixed4 frag (v2f i) : SV_Target
            {
                
                float2 uv = i.uv;                
                float3 finalColor = _Color.rgb*_Emission;
                float mask = DrawCurvedRect(uv,4,_EdgeMask); 
                float flicker = Flicker01(0.1,_RandomSpeed,_ShinnyAmplitude);
                float net = DrawNet(uv,float2(_U,_V),_Grid_Width);
                
                
                float netGrid =net*_Grid_Opacity*mask;
                float bg = mask*_BG_Opacity*0.5;

                

                return fixed4(finalColor,saturate(netGrid+bg)*flicker);
            }
            ENDCG
        }
    }
}
